﻿<?php
/**
 * This is the file to define ContentList.
 * 
 * @author	Justin Yoo
 * @version	0.0.1.0001
 * @package	KYR.Components
 * 
 */

//	Checks if this file is called within the framework; otherwise terminates.
if (!defined("IS_FRAMEWORK") || !IS_FRAMEWORK)	header("Location: http://".$_SERVER["HTTP_HOST"]."/error/404");

//	Imports relevant files.
require_once CORE_PATH."KYR.Core.BaseComponent.php";

/**
 * This is class ContentList that controls the list of contents.
 *
 * Controls the list of contents.
 * 
 * @author	Justin Yoo
 * @package	KYR.Components
 * 
 */
class ContentList extends BaseComponent
{
	/**
	 * Initiates the ContentList class.
	 *
	 * @access	public
	 * @param	Request	$request	Request object that contains page request details.
	 * @param	int		$objectID	Either moduleID or widgetID that calls the component.
	 * @param	string	$mappedBy	Flag whether the component is called by a module or widget.
	 * @param	int		$statusCode	Page status code.
	 * @exception					Throws an Exception while initialising the class.
	 *
	 */
	public function __construct($request, $objectID, $mappedBy, $statusCode = 200)
	{
		parent::__construct($request, $objectID, $mappedBy, $statusCode);
		$this->componentName = __CLASS__;

		if ($this->statusCode == 200)
		{
			try
			{
				$this->CheckComponent_();
				if (!$this->existComponent)
					throw new Exception("Component not found");
			}
			catch (Exception $ex) { throw $ex; }
		}
		else
			$this->existComponent = true;
		//	Creates controls for this component.
		$this->CreateControls_(); 
	}
	
	/**
	 * Creates a set of controls to be used in templates called by the component.
	 *
	 * @access	protected
	 *
	 */
	protected function CreateControls_()
	{
		//	Create controls for this component.
		$this->controls_ = array(
			"title"		=> null,
			"contents"	=> array(
				"contentID"		=> null,
				"title"			=> null,
				"uri"			=> null,
				"content"		=> null,
				"dateCreated"	=> null
				)
			);
	}
	
	/**
	 * Processes the component.
	 *
	 * @access	public
	 * @param	array	$variables	Set of data to be stored by the component.
	 * @exception					Throws an Exception while processing the component.
	 *
	 */
	public function ProcessComponent(&$variables)
	{
		//	Sets the controls.
		$this->SetControls_($variables);
		//	Processes the parent method.
		try { $this->ProcessComponent_($variables); } catch (Exception $ex) { throw $ex; }
		//	Processes handlers
		if (isset($this->handlers) && count($this->handlers) > 0)
			foreach ($this->handlers as $handler)
				$handler->ProcessHandler($variables);
	}
	
	/**
	 * Sets the set of controls to be used in templates called by the component.
	 *
	 * @access	protected
	 * @param	array	$variables	Set of data generated by the component.
	 * @exception					Throws an Exception while setting data onto the controls.
	 *
	 */
	protected function SetControls_(&$variables)
	{
		//	Gets the list of contents.
		try { $contents = $this->_GetContents(); }
		catch (Exception $ex) { throw $ex; }

		//	Sets the list of contents to the controls.
		$this->controls_["title"]		= "List of Contents";
		$this->controls_["contents"]	= $contents;
		//Debug::PrintScreen("pre", $this->controls_);

		//	Adds the controls for this component.
		$variables["controls"][$this->componentName] = $this->controls_;
	}
	
	/**
	* Gets the list of contents.
	*
	* @access	private
	* @return	array		Returns the list of contents.
	* @exception			Throws an Exception while retrieving data from the database.
	*
	*/
	private function _GetContents()
	{
		try
		{
			$db	= null;
			$contents = null;
			if ($this->statusCode == 200)
			{
				$db = new Database("MySQL");
				$qry = "CALL cms_GetContents()";
				$db->Query($qry);

				while ($content = $db->Fetch())
					$contents[] = $content;

				return $contents;
			}
		}
		catch (Exeption $ex) { throw $ex; }
	}
}
?>
